{extend name="public/base" /}

{block name="content"}
<div class="container my-3 my-md-4">
    <div class="row">
        <div class="col-lg-4">
            {include file="public/sidenav" /}
        </div>
        <div class="col-lg-8 mt-3 mt-lg-0">
            <form id="change-pwd-form" method="post">
                {:token()}
                <input type="hidden" name="id" value="{$Think.session.user.id}">
                <div class="card">
                    <div class="card-header">
                        <strong>修改密码</strong>
                    </div>
                    <div class="card-body">
                        <div class="form-group">
                            <label for="old_password">旧密码</label>
                            <input class="form-control" name="old_password" id="old_password" type="password" placeholder="输入旧密码">
                        </div>
                        <div class="form-group">
                            <label for="new_password">新密码</label>
                            <input class="form-control" name="new_password" id="new_password" type="password" placeholder="输入新密码">
                        </div>
                        <div class="form-group">
                            <label for="new_password_confirm">确认新密码</label>
                            <input class="form-control" name="new_password_confirm" id="new_password_confirm" type="password" placeholder="确认新密码">
                        </div>
                    </div>
                    <div class="card-footer">
                        <button class="btn btn-success" type="button" id="sub">提交</button>
                        <button class="btn btn-secondary" type="reset">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
{/block}

{block name="script"}
<script>
    toastr.options = {
        "closeButton": true,
        "positionClass": "toast-top-center",
        "timeOut": "1500"
    };

    $('#sub').on('click',function () {
        $.ajax({
            type: 'post',
            url: "{:url('User/changePwdDo')}",
            data: $('#change-pwd-form').serialize(),
            dataType: 'json',
            success: function (data) {
                switch (data.status) {
                    case 1:
                        $('input[name="__token__"]').val(data.token);
                        toastr.success(data.message);
                        setTimeout(function () {
                            window.location.href = "{:url('User/logout')}";
                        },1500);
                        break;
                    case 0:
                    case -1:
                    case -2:
                        $('input[name="__token__"]').val(data.token);
                        toastr.warning(data.message);
                        break;
                }
            }
        })
    })
</script>
{/block}